package com.ruoyi.whole.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.AutofillUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.MapperUtils;
import com.ruoyi.whole.domain.DmsDealerVehicleInfo;
import com.ruoyi.whole.domain.DmsDealerWholePurchase;
import com.ruoyi.whole.domain.DmsDealerWholePurchaseDetail;
import com.ruoyi.whole.domain.DmsWholeVehicleDelivery;
import com.ruoyi.whole.domain.vo.DmsWholeVehicleDeliveryVo;
import com.ruoyi.whole.mapper.DmsWholeVehicleDeliveryMapper;
import com.ruoyi.whole.service.IDmsDealerVehicleInfoService;
import com.ruoyi.whole.service.IDmsDealerWholePurchaseDetailService;
import com.ruoyi.whole.service.IDmsDealerWholePurchaseService;
import com.ruoyi.whole.service.IDmsWholeVehicleDeliveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 车辆发运Service业务层处理
 * 
 * @author liuchuanfa
 * @date 2024-03-08
 */
@Service
public class DmsWholeVehicleDeliveryServiceImpl extends ServiceImpl<DmsWholeVehicleDeliveryMapper,DmsWholeVehicleDelivery> implements IDmsWholeVehicleDeliveryService
{
    @Autowired
    private DmsWholeVehicleDeliveryMapper dmsWholeVehicleDeliveryMapper;

    @Autowired
    private AutofillUtil autofillUtil;

    @Autowired
    private IDmsDealerWholePurchaseService iDmsDealerWholePurchaseService;

    @Autowired
    private IDmsDealerWholePurchaseDetailService iDmsDealerWholePurchaseDetailService;

    @Autowired
    private IDmsDealerVehicleInfoService iDmsDealerVehicleInfoService;

    /**
     * 根据id查询发运信息以及发运信息对应的采购订单信息
     * 
     * @param id 车辆发运主键
     * @return 车辆发运
     */
    @Override
    public DmsWholeVehicleDelivery selectDmsWholeVehicleDeliveryById(Long id)
    {
        //根据id查询发运单信息
        DmsWholeVehicleDelivery dmsWholeVehicleDelivery = dmsWholeVehicleDeliveryMapper.selectDmsWholeVehicleDeliveryById(id);
        Long purchaseId = dmsWholeVehicleDelivery.getPurchaseId();
        //根据采购订单id，查询详细的采购订单信息
        DmsDealerWholePurchase dmsDealerWholePurchase = iDmsDealerWholePurchaseService.getDmsDealerWholePurchaseById(purchaseId);
        //类型转换
        DmsWholeVehicleDeliveryVo dmsWholeVehicleDeliveryVo = MapperUtils.mapperBean(dmsWholeVehicleDelivery, DmsWholeVehicleDeliveryVo.class);
        //塞值
        List<DmsDealerWholePurchase> list = new ArrayList<>();
        list.add(dmsDealerWholePurchase);
        dmsWholeVehicleDeliveryVo.setDmsDealerWholePurchaseList(list);
        return dmsWholeVehicleDeliveryVo;
    }

    /**
     * 查询车辆发运列表
     * 
     * @param dmsWholeVehicleDelivery 车辆发运
     * @return 车辆发运
     */
    @Override
    public List<DmsWholeVehicleDelivery> selectDmsWholeVehicleDeliveryList(DmsWholeVehicleDelivery dmsWholeVehicleDelivery)
    {
        return dmsWholeVehicleDeliveryMapper.selectDmsWholeVehicleDeliveryList(dmsWholeVehicleDelivery);
    }

    /**
     * 新增车辆发运
     * 
     * @param dmsWholeVehicleDelivery 车辆发运
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDmsWholeVehicleDelivery(DmsWholeVehicleDelivery dmsWholeVehicleDelivery)
    {
        //新建发运订单时只能是已发运状态
        if (!dmsWholeVehicleDelivery.getStatus().equals(1)){
            throw new ServiceException("新建发运订单状态只能是已发运！");
        }

        //新增自动填充
        autofillUtil.autoFillCreate(dmsWholeVehicleDelivery);
        //插入发运时间
        dmsWholeVehicleDelivery.setForwardingDate(new Date());
        dmsWholeVehicleDeliveryMapper.insertDmsWholeVehicleDelivery(dmsWholeVehicleDelivery);

        //1. 修改采购状态为 待收货
        iDmsDealerWholePurchaseService.lambdaUpdate()
                .set(DmsDealerWholePurchase::getPurchaseStatus,6)
                .eq(DmsDealerWholePurchase::getId,dmsWholeVehicleDelivery.getPurchaseId())
                .update();

        //2. 修改车辆状态为 经销商在途
        //①根据采购订单号，查找车辆vin码
        DmsDealerWholePurchaseDetail dmsDealerWholePurchaseDetail = new DmsDealerWholePurchaseDetail();
        dmsDealerWholePurchaseDetail.setPurchaseId(dmsWholeVehicleDelivery.getPurchaseId());
        List<String> vinList = iDmsDealerWholePurchaseDetailService.lambdaQuery()
                .eq(DmsDealerWholePurchaseDetail::getPurchaseId, dmsWholeVehicleDelivery.getPurchaseId()).list()
                .stream().map(i -> i.getVin()).collect(Collectors.toList());

        //②修改车辆状态
        iDmsDealerVehicleInfoService.lambdaUpdate()
                .set(DmsDealerVehicleInfo::getVehicleStatus,1)
                .in(DmsDealerVehicleInfo::getVin,vinList)
                .update();

        return 1;
    }

    /**
     * 修改车辆发运
     * 
     * @param dmsWholeVehicleDelivery 车辆发运
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDmsWholeVehicleDelivery(DmsWholeVehicleDelivery dmsWholeVehicleDelivery)
    {
        //状态不可逆
        DmsWholeVehicleDelivery dmsWholeVehicleDeliveryDB = selectDmsWholeVehicleDeliveryById(dmsWholeVehicleDelivery.getId());
        Integer statusDB = dmsWholeVehicleDeliveryDB.getStatus();
        Integer status = dmsWholeVehicleDelivery.getStatus();
        if (status-statusDB!=1){
            throw new ServiceException("状态不可逆 且 不可跨越！");
        }

        //修改自动填充
        autofillUtil.autoFillUpdate(dmsWholeVehicleDelivery);
        //采购表添加 审核人、时间、人名
        dmsWholeVehicleDelivery.setAuditorId(SecurityUtils.getLoginUser().getUser().getUserId());
        dmsWholeVehicleDelivery.setAuditorName(SecurityUtils.getLoginUser().getUser().getUserName());
        dmsWholeVehicleDelivery.setAuditorTime(new Date());
        dmsWholeVehicleDelivery.setForwardingDate(new Date());
        dmsWholeVehicleDeliveryMapper.updateDmsWholeVehicleDelivery(dmsWholeVehicleDelivery);

        //当已签收
        if (dmsWholeVehicleDelivery.getStatus().equals(3)){
            //1. 修改采购订单状态为已签收
            iDmsDealerWholePurchaseService.lambdaUpdate()
                    .set(DmsDealerWholePurchase::getPurchaseStatus,7)
                    .eq(DmsDealerWholePurchase::getId,dmsWholeVehicleDelivery.getPurchaseId())
                    .update();

            // 2. 修改车辆状态为 经销商在途
            //①根据采购订单号，查找车辆vin码
            DmsDealerWholePurchaseDetail dmsDealerWholePurchaseDetail = new DmsDealerWholePurchaseDetail();
            dmsDealerWholePurchaseDetail.setPurchaseId(dmsWholeVehicleDelivery.getPurchaseId());
            List<String> vinList = iDmsDealerWholePurchaseDetailService.lambdaQuery()
                    .eq(DmsDealerWholePurchaseDetail::getPurchaseId, dmsWholeVehicleDelivery.getPurchaseId()).list()
                    .stream().map(i -> i.getVin()).collect(Collectors.toList());

            //②修改车辆状态
            iDmsDealerVehicleInfoService.lambdaUpdate()
                    .set(DmsDealerVehicleInfo::getVehicleStatus,2)
                    .set(DmsDealerVehicleInfo::getInventoryType,"店端")
                    .in(DmsDealerVehicleInfo::getVin,vinList)
                    .update();
        }

        return 1;
    }

    /**
     * 批量删除车辆发运
     * 
     * @param ids 需要删除的车辆发运主键
     * @return 结果
     */
    @Override
    public int deleteDmsWholeVehicleDeliveryByIds(Long[] ids)
    {
        return dmsWholeVehicleDeliveryMapper.deleteDmsWholeVehicleDeliveryByIds(ids);
    }

    /**
     * 删除车辆发运信息
     * 
     * @param id 车辆发运主键
     * @return 结果
     */
    @Override
    public int deleteDmsWholeVehicleDeliveryById(Long id)
    {
        return dmsWholeVehicleDeliveryMapper.deleteDmsWholeVehicleDeliveryById(id);
    }

    /**
     * 根据发运单号，查询采购订单信息
     *
     * @param id
     * @return {@link DmsDealerWholePurchase }
     * @author liuchuanfa
     * @since 2024/03/08
     */
    @Override
    public DmsDealerWholePurchase getPurchaseListByDeliveryId(Long id) {
        //根据发运单号查询采购订单id
        DmsWholeVehicleDelivery dmsWholeVehicleDelivery = dmsWholeVehicleDeliveryMapper.selectDmsWholeVehicleDeliveryById(id);
        Long purchaseId = dmsWholeVehicleDelivery.getPurchaseId();
        //根据采购订单id，查询详细的采购订单信息
        return iDmsDealerWholePurchaseService.getDmsDealerWholePurchaseById(purchaseId);
    }
}
